package Shift;

import Square.*;
import java.awt.image.BufferedImage;

public class Father
{
    public move[] cells = new move[4];
    protected Condi[] condis; //旋转的状态
    protected int count = 10000; //声明旋转次数

    public Father() {}
    public Father(Father other) //复制构造函数
    {
        this.cells = new move[other.cells.length];
        for (int i = 0; i < other.cells.length; i++)
        {
            this.cells[i] = new move(other.cells[i].getRow(), other.cells[i].getCol(), other.cells[i].getImage());
        }
        this.condis = new Condi[other.condis.length]; //创建新的Condi数组
        for (int i = 0; i < other.condis.length; i++)
        {
            this.condis[i] = new Condi(other.condis[i]); //使用复制构造函数复制每个Condi对象
        }
        this.count = other.count;
    }

    public void moveLeft() //左移方法
    {
        for (move cell : cells)
            cell.left();
    }

    public void moveRight() //右移方法
    {
        for (move cell : cells)
            cell.right();
    }

    public void moveDrop() //单元格下落
    {
        for (move cell : cells)
            cell.down();
    }

    public static Father randomOne() //随机生成四方格
    {
        int num = (int) (Math.random() * 7);
        Father f = null;
        switch (num)
        {
            case 0:
                f = new A();
                break;
            case 1:
                f = new B();
                break;
            case 2:
                f = new C();
                break;
            case 3:
                f = new D();
                break;
            case 4:
                f = new E();
                break;
            case 5:
                f = new F();
                break;
            case 6:
                f = new G();
                break;
        }
        return f;
    }

    public void rotateRight() //顺时针旋转方法
    {
        if (condis.length == 0) return;
        count++; //旋转次数+1
        Condi s = condis[count % condis.length];
        move cell = cells[0];
        int row = cell.getRow();
        int col = cell.getCol();
        cells[1].setRow(row + s.row1);
        cells[1].setCol(col + s.col1);
        cells[2].setRow(row + s.row2);
        cells[2].setCol(col + s.col2);
        cells[3].setRow(row + s.row3);
        cells[3].setCol(col + s.col3);
    }

    public void rotateLeft() //逆时针旋转方法
    {
        if (condis.length == 0) return;
        count--; //旋转次数-1
        Condi s = condis[count % condis.length];
        move cell = cells[0];
        int row = cell.getRow();
        int col = cell.getCol();
        cells[1].setRow(row + s.row1);
        cells[1].setCol(col + s.col1);
        cells[2].setRow(row + s.row2);
        cells[2].setCol(col + s.col2);
        cells[3].setRow(row + s.row3);
        cells[3].setCol(col + s.col3);
    }

    protected class Condi //四方格旋转状态的内部类
    {
        int row0, col0, row1, col1, row2, col2, row3, col3; //存储四方格各元素的位置

        public Condi() {}
        public Condi(int row0, int col0, int row1, int col1, int row2, int col2, int row3, int col3)
        {
            this.row0 = row0;
            this.col0 = col0;
            this.row1 = row1;
            this.col1 = col1;
            this.row2 = row2;
            this.col2 = col2;
            this.row3 = row3;
            this.col3 = col3;
        }
        // 复制构造函数
        public Condi(Condi other)
        {
            this.row0 = other.row0;
            this.col0 = other.col0;
            this.row1 = other.row1;
            this.col1 = other.col1;
            this.row2 = other.row2;
            this.col2 = other.col2;
            this.row3 = other.row3;
            this.col3 = other.col3;
        }

        public int getRow0() {
            return row0;
        }
        public void setRow0(int row0) {
            this.row0 = row0;
        }
        public int getCol0() {
            return col0;
        }
        public void setCol0(int col0) {
            this.col0 = col0;
        }

        public int getRow1() {
            return row1;
        }
        public void setRow1(int row1) {
            this.row1 = row1;
        }
        public int getCol1() {
            return col1;
        }
        public void setCol1(int col1) {
            this.col1 = col1;
        }

        public int getRow2() {
            return row2;
        }
        public void setRow2(int row2) {
            this.row2 = row2;
        }
        public int getCol2() {
            return col2;
        }
        public void setCol2(int col2) {
            this.col2 = col2;
        }

        public int getRow3() {
            return row3;
        }
        public void setRow3(int row3) {
            this.row3 = row3;
        }
        public int getCol3() {
            return col3;
        }
        public void setCol3(int col3) {
            this.col3 = col3;
        }

        @Override
        public String toString()
        {
            return "State{" +
                    "row0=" + row0 +
                    ", col0=" + col0 +
                    ", row1=" + row1 +
                    ", col1=" + col1 +
                    ", row2=" + row2 +
                    ", col2=" + col2 +
                    ", row3=" + row3 +
                    ", col3=" + col3 +
                    '}';
        }
    }
}
